package org.serverct.finalfight.internal.game.hadesgame;

import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarFlag;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.scheduler.BukkitRunnable;
import org.serverct.finalfight.FinalFight;
import org.serverct.finalfight.game.hadesgame.EventType;
import org.serverct.finalfight.internal.game.basic.BasicGame;
import org.serverct.finalfight.internal.game.basic.GameState;
import org.serverct.finalfight.internal.info.SimpleInfoHandler;
import org.serverct.finalfight.stat.StatManager;
import org.serverct.finalfight.util.CustomSettings;
import org.serverct.finalfight.util.StringPair;

import java.util.UUID;

public abstract class BasicEvent {
    public static int noticeDuration = CustomSettings.config.getInt("noticeDuration");
    public BasicGame game;
    public EventType type;
    public int noticeTicking;
    public BossBar bar;
    public double factor;

    public BasicEvent(BasicGame game, EventType type) {
        this.game = game;
        this.type = type;
        this.noticeTicking = 0;
        this.factor = 0;
        new BukkitRunnable(){
            @Override
            public void run() {
                BasicEvent.this.notice();
            }
        }.runTaskLater(FinalFight.plugin,1L);
    }

    public void execute() {
    }

    public void notice() {
        this.bar = Bukkit.createBossBar(this.getNoticeTitle(), BarColor.RED, BarStyle.SOLID, BarFlag.PLAY_BOSS_MUSIC);
        this.bar.setProgress(1.0);
        this.bar.removeFlag(BarFlag.PLAY_BOSS_MUSIC);
        for (UUID uuid : this.game.players.keySet()) {
            this.bar.addPlayer(Bukkit.getPlayer(uuid));
        }

        SimpleInfoHandler.broadcastMessage(this.game, false, false, "newevent_1");
        SimpleInfoHandler.broadcastMessage(this.game, false, false, "newevent_2", new StringPair("%event%", this.type.getName(this.factor)));
        SimpleInfoHandler.broadcastMessage(this.game, false, false, "newevent_3", new StringPair("%description%", this.type.getDescription()));
        SimpleInfoHandler.broadcastMessage(this.game, false, false, "newevent_4");

        new BukkitRunnable() {
            @Override
            public void run() {
                BasicEvent.this.bar.setTitle(BasicEvent.this.getNoticeTitle());
                BasicEvent.this.bar.setProgress(1.0 - (double) BasicEvent.this.noticeTicking / noticeDuration);

                if (BasicEvent.this.noticeTicking >= noticeDuration) {
                    BasicEvent.this.bar.removeAll();
                    BasicEvent.this.execute();
                    for (UUID uuid : BasicEvent.this.game.players.keySet()) {
                        StatManager.stats.get(uuid).addExperience(BasicEvent.this.type);
                    }
                    this.cancel();
                }
                BasicEvent.this.noticeTicking++;

                if (BasicEvent.this.game.state != GameState.RUNNING) {
                    BasicEvent.this.bar.removeAll();
                    this.cancel();
                }
            }
        }.runTaskTimer(FinalFight.plugin, 0L, 20L);
    }

    public String getNoticeTitle() {
        return CustomSettings.getLanguageWithoutPrefix("randomeventnoticebar",
                new StringPair("%event%", this.type.getName(this.factor)),
                new StringPair("%left%", String.valueOf(noticeDuration - this.noticeTicking)));
    }
}
